package com.itmk.web.comments.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itmk.utils.ResultUtils;
import com.itmk.utils.ResultVo;
import com.itmk.web.comments.entity.CommentListParm;
import com.itmk.web.comments.entity.Comments;
import com.itmk.web.comments.service.CommentsService;

import com.itmk.web.goods.service.GoodsService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/comment")
public class CommentController {

    @Autowired
    private CommentsService commentService;


    @PostMapping("/add")
    public ResultVo addComment(@RequestBody Comments comment) {
        // 1. 检查订单是否已经评论过
        boolean isOrderCommented = commentService.isOrderCommented(comment.getOrderId());
        if (isOrderCommented) {
            return ResultUtils.error("该订单已评论，不能重复评论");
        }
        comment.setCreatedAt(new Date());
        comment.setUpdatedAt(new Date());
        if (commentService.save(comment)) {
            return ResultUtils.success("评论成功");
        }
        return ResultUtils.error("评论失败");
    }


    // 根据 buyerId 查询评论，并返回买家的头像和用户名，查看我的评论
    @GetMapping("/getAllComments/{buyerId}")
    public ResultVo getCommentsByBuyerId(@PathVariable Long buyerId) {
        List<Comments> commentsList = commentService.getCommentsByBuyerId(buyerId);
        System.out.println(commentsList);

        return ResultUtils.success("查询成功", commentsList);
    }

    // 根据 sellerId 查询评论，并返回评论买家的头像和用户名，查看卖家历史评论
    @GetMapping("/getSellerComments/{sellerId}")
    public ResultVo getCommentsBySellerId(@PathVariable Long sellerId) {
        List<Comments> commentsList = commentService.getCommentsBySellerId(sellerId);
        System.out.println(commentsList);

        return ResultUtils.success("查询成功", commentsList);
    }

    // 获取评论列表（分页+条件查询）
    @GetMapping("/getList")
    public ResultVo getList(CommentListParm parm) {
        // 创建查询包装器
        QueryWrapper<Comments> query = new QueryWrapper<>();
        // 设置查询条件
        query.lambda()
                .eq(parm.getCommentId() != null, Comments::getCommentId, parm.getCommentId())
                .orderByDesc(Comments::getCreatedAt);
        // 创建分页对象
        IPage<Comments> page = new Page<>(parm.getCurrentPage(), parm.getPageSize());
        // 执行分页查询
        IPage<Comments> list = commentService.page(page, query);
        // 返回结果
        return ResultUtils.success("查询成功", list);
    }

    @DeleteMapping("/delete/{id}")
    public ResultVo deleteComment(@PathVariable Long id) {
        boolean removed = commentService.removeById(id);
        if (removed) {
            return ResultUtils.success("删除成功");
        } else {
            return ResultUtils.error("删除失败");
        }
    }


}
